package com.cylise.coolweather.service;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.annotation.IntDef;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.cylise.coolweather.Alarms;
import com.cylise.coolweather.R;
import com.cylise.coolweather.Weather;
import com.cylise.coolweather.WeatherActivity;
import com.cylise.coolweather.WeatherWidget;
import com.cylise.coolweather.util.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AutoUpdateService extends Service {
    private Weather weathers;
    public AutoUpdateService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("weather","onStartCommand");
        SharedPreferences sharedPreferences = getSharedPreferences("weather",MODE_PRIVATE);
        String weatherId = sharedPreferences.getString("weatherId","");

        if (weatherId!=null){
            String url="https://free-api.heweather.com/v5/weather?key=856153fdbb8d4dcc995cebea8a493b7e&city=";
            HttpUtil.sendOKHttpRequest(url + weatherId, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String body = response.body().string();

                    SharedPreferences sharedPreferences = getSharedPreferences("weather", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("weathers",body);
                    editor.apply();

                    WeatherActivity.updateWidget(getApplicationContext());

                    Intent i = new Intent("com.cylise.coolweather.refresh");
                    AutoUpdateService.this.sendBroadcast(i);
                    Log.d("sendbroad","发送广播");
                }
            });


        }

        String url="http://guolin.tech/api/bing_pic";

        HttpUtil.sendOKHttpRequest(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String body = response.body().string();
                SharedPreferences.Editor editor = getSharedPreferences("weather",MODE_PRIVATE).edit();
                editor.putString("bingPic",body);
                editor.apply();
                //final Bitmap bitmap = returnBitMap(body);

            }
        });

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int times= 1000*60*60;
        long triggerAtTime = SystemClock.elapsedRealtime() + times;
        Intent in=new Intent(this,AutoUpdateService.class);
        PendingIntent pi= PendingIntent.getService(this,0,in,0);
        alarmManager.cancel(pi);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);

        return super.onStartCommand(intent, flags, startId);
    }
}
